/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Login;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.StringTokenizer;
import oracle.jdbc.pool.OracleDataSource;

/**
 *
 * @author SETYO RESMI
 */
public class Koneksi {

    private Connection conn;
    private boolean ok;
    private String jdbcURL = "jdbc:oracle:thin:@localhost:1521:XE";
    private String user = "rplt";
    private String password = "rplt";

    public boolean getDBConnection(Login log) {
        try {

            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL("jdbc:oracle:thin:@localhost:1521:XE");
            conn = ds.getConnection(log.getUserName(), log.getPassword());
            System.out.println("Koneksi Berhasil");
            ok = true;
        } catch (SQLException ex) {
            System.out.println("Koneksi Gagal");
            ok = false;
        }
        return ok;


    }

    public Connection getConnection() {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection(jdbcURL,user,password);
            System.out.println("koneksi berhasil");
        } catch (Exception ex) {
            System.out.println("message: " + ex.getMessage());
        }
        return conn;
    }

    public static String[] openConfiguration(File file) throws FileNotFoundException, IOException {
        FileInputStream input = new FileInputStream(file);
        String teks = new String();
        int a = 0;
        while ((a = input.read()) != -1) {
            teks += (char) a;
        }
        StringTokenizer tokens = new StringTokenizer(teks, ";");
        String result[] = new String[tokens.countTokens()];


        for (int i = 0; i < result.length; i++) {
            result[i] = tokens.nextToken();

        }
        return result;
    }

    public static void main(String[] args) throws FileNotFoundException, IOException {

        Koneksi uji = new Koneksi();
        String result[] = Koneksi.openConfiguration(new File("src/Login/dbconfig"));
        System.out.println("Host:" + result[0]);
        System.out.println("Username:" + result[1]);
        System.out.println("Password:" + result[2]);
    }
}
